package com.cyys.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @description gainWxLogin
 * @date 2022-06-08 12:35
 */
@RestController
@RequestMapping("/gainWxLogin")
public class gainWxLogin {
    public static void main(String[] args) {
     gainWxLogin("053116200aIV0O1QG6300B0RXw011625");
    }

    public static JSONObject gainWxLogin(String code){
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + "wx7a54374e32c3ab51" + "&secret=" + "c4b51f78f005e929df3f2de392bf0cdf" + "&js_code=" + code + "&grant_type=authorization_code";
        JSONObject jsonObject = null;
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpGet get = new HttpGet(url.toString());    //构建一个GET请求
            HttpResponse response = client.execute(get);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println(content);//打印返回的信息
            jsonObject = JSONObject.parseObject(content);//把信息封装为json
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


}
